//author:kakake
class tsMenuController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn(),
        WorkId: 1
    }

    public service: tsMenuService;
    public dao: tsMenuDao;

    constructor() {
        this.data.WorkId = TSLobsterAPI.Program.GetWorkId();

        this.service = new tsMenuService(this.data.WorkId);
        this.dao = new tsMenuDao(this.data.WorkId);

    }

    /**获取菜单 */
    getMenuData() {
        let modulelist = this.dao.getBaseModule();
        let menulist = this.dao.getBaseMenu();
        return { modulelist: modulelist, menulist: menulist };
    }

    /**保存菜单 */
    saveMenu(menu) {
        let model: BaseMenu_Model = <BaseMenu_Model>{};
        model.MenuId = menu.MenuId;
        model.SortId = menu.SortId;
        model.Name = menu.Name;
        model.DllName = menu.SoftName;
        model.FunName = menu.ControllerName;
        model.FunWcfName = menu.ViewName;
        model.ModuleId = menu.ModuleId;
        model.PMenuId = menu.PMenuId;
        model.MenuToolBar = menu.MenuToolBar;
        model.MenuLookBar = menu.MenuLookBar;
        model.Memo = menu.Memo;
        model.Image = menu.Image;
        model.UrlId = menu.UrlId;
        model.UrlName = menu.UrlName;
        model.BindSQL = menu.BindSQL;

        let menu_orm = new BaseMenu_ORM(this.data.WorkId);
        menu_orm.Save(model);
    }

    /**删除菜单 */
    deleteMenu(menuId) {
        this.dao.deleteMenu(menuId);
    }

    /**获取模块 */
    getModule(moduleId) {
        var currModule = this.dao.getCurrModule(moduleId);
        return { currModule: currModule };
    }

    /**删除模块 */
    deleteModule(moduleId) {
        this.dao.deleteModule(moduleId);
    }

    /**保存模块 */
    saveModule(bodydata) {
        this.dao.saveModule(bodydata);
    }

    /**获取角色 */
    getGroup(groupId) {
        let group_orm = new BaseGroup_ORM(this.data.WorkId);
        let group = group_orm.GetModel(groupId);
        return { currGroup: group };
    }

    /**删除角色 */
    deleteGroup(groupId) {
        this.dao.deleteGroup(groupId);
    }

    /**保存角色 */
    saveGroup(group) {
        let group_orm = new BaseGroup_ORM(this.data.WorkId);
        group_orm.Save(group);
    }

    /**获取机构列表 */
    getWorkerData() {
        var workers = this.dao.getWorkerData();
        return { workers: workers };
    }

    /**获取角色数据 */
    getGroupData(currWorkId) {
        let group_orm = new BaseGroup_ORM(this.data.WorkId);
        var grouplist = group_orm.GetList();
        return { grouplist: grouplist };
    }

    /**获取角色菜单 */
    getGroupMenuData(groupId) {
        var modulelist = this.dao.getBaseModule();
        var menulist = this.dao.getBaseMenu();
        var groupmenulist = this.dao.getGroupMenuList(groupId);

        return { modulelist: modulelist, menulist: menulist, groupmenulist: groupmenulist };
    }

    /**设置角色的菜单权限 */
    setGroupMenu(data) {
        var groupId = data.groupId;
        var menuIds = data.menuIds;

        //删除掉原来的菜单
        this.dao.deleteGroupMenu(groupId);
        //循环添加新的菜单
        var array_menuid = TSLobsterAPI.Utils.StringToArray(menuIds, ',');
        array_menuid.forEach(menuid => {
            this.service.addPMenuId(groupId, menuid);
            this.dao.saveUserWorkGroup(groupId, menuid);
        })

    }


}